import '../sass/common.scss';
import { initHeaderFooter } from './gopLib/initHeaderFooter';
import * as tools from './tools';
jQuery.support.cors = true;
$(function () {
    initHeaderFooter('orderSearch');
    let order = tools.getArg('order');
    if (order.length > 0) {
        searchOrder(order);
        initSearch(order);
    }

    $(document).on('click', '.os-info-head', function () {
        $(this).parent().toggleClass('active');
    })

    $(document).on('click', '.os_btn', function () {
        const order = $('#s_orders').val()
        if (order.length <= 0) {
            alert('请输入您要查询的运单号');
        } else {
            searchOrder(order)
        }
    })
})


function searchOrder(order) {
    $.ajax({
        url: tools.axUrl + `method=getRouteData&params=${order}&appKey=OPEN_PLATFORM_APPKEY&v=1.0`,
        type: 'post',
        success: function (res) {
            if (res.code == '200') {
                renderRes(res.data);
            }
        }
    })
}

function initSearch(order) {
    $('#s_orders').val(order);
}

function renderRes(data) {
    let resHtml = data.map(val => {
        let newest = '';
        let routingData2Html = {};
        let isTitle = false;
        if (val.routingData2.length > 0) {
            newest = '';
        } else {
            newest = '无';
        }
        
        val.routingData2.map((val) => {
            if(!isTitle && val.remark1) {
                newest = val.remark1;
                isTitle = true;
            }
            if (val && val.remark1) {
                let weeks = tools.getDay(val.waybillTime);
                let waybillTime = val.waybillTime.split(' ')[0];
                let time = val.waybillTime.split(' ')[1];
                if (!routingData2Html[waybillTime]) {
                    routingData2Html[waybillTime] = {}
                    routingData2Html[waybillTime].title = waybillTime + ' ' + weeks;
                    routingData2Html[waybillTime].str = `<p>${time}<span>${val.remark1}</span></p>`;
                } else {
                    routingData2Html[waybillTime].str += `<p>${time}<span>${val.remark1}</span></p>`;
                }
            }

        })
        let routHtml = '';
        for (let title in routingData2Html) {
            routHtml += `
            <div class="os-info">
                <div class="os-info-head">${routingData2Html[title].title}</div>
                <div class="os-info-detail">
                    ${routingData2Html[title].str}
                </div>
            </div>
            `
        }
        return `
            <div class="os-result-item">
            <div class="os-item-head">
                <p>单&#12288&#12288号:
                    <span>${val.param}</span>
                </p>
                <p>最新动态:
                    <span>${newest}</span>
                </p>
            </div>
            <div class="os-item-body">
                <div class="os-info-list">
                    ${routHtml}
                </div>
            </div>
        </div>
        `
    });
    $('.os-result-list').addClass('dis_show').html(resHtml.join(''));
}